package com.yydm.serviceorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.wxpay.sdk.WXPayUtil;
import com.yydm.commonutils.ordervo.AutomobileVo;
import com.yydm.commonutils.ordervo.UcenterVo;
import com.yydm.serviceorder.client.AutoClient;
import com.yydm.serviceorder.client.UcenterClient;
import com.yydm.serviceorder.entity.AutomobileOrder;
import com.yydm.serviceorder.entity.AutomobileOrderLog;
import com.yydm.serviceorder.mapper.AutomobileOrderMapper;
import com.yydm.serviceorder.service.IAutomobileOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yydm.serviceorder.utils.HttpClient;
import com.yydm.serviceorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yydm
 * @since 2022-07-14
 */
@Service
public class AutomobileOrderServiceImpl extends ServiceImpl<AutomobileOrderMapper, AutomobileOrder> implements IAutomobileOrderService {

    @Autowired
    private AutoClient autoClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private IAutomobileOrderService automobileOrderService;

    //    生成订单的方法
    @Override
    public String createOrder(String automobileId, Integer memberIdByJwtToken) {
//        通过远程调用根据用户id获取用户信息
        UcenterVo ucenterVo = ucenterClient.getUserInfoOrder(memberIdByJwtToken);
//        通过远程调用根据汽车id获取汽车信息
        AutomobileVo automobileInfo = autoClient.getAutomobileInfo(automobileId);
//        通过远程调用根据id获取创始人信息
        AutomobileVo founderName = autoClient.getFounderName(automobileInfo.getFounderId());

//        创建AutomobileOrder对象传入数据
        AutomobileOrder automobileOrder = new AutomobileOrder();
        automobileOrder.setOrderId(OrderNoUtil.getOrderNo());  //订单号
        automobileOrder.setAutomobileId(automobileId);  //汽车id
        automobileOrder.setAutomobileName(automobileInfo.getProductName());
        automobileOrder.setFounderName(founderName.getFounderName());
        automobileOrder.setFileName(automobileInfo.getFileName());
//        automobileOrder.setFounderName(ucenterVo.getUsername());
        automobileOrder.setUcenterId(ucenterVo.getId());
        automobileOrder.setUcenterName(ucenterVo.getUsername());
        automobileOrder.setOrderPrice(automobileInfo.getProductPrice());
        automobileOrder.setOrderType(1);
        automobileOrder.setOrderStatus(2);
        automobileOrder.setCreateTime(automobileInfo.getCreateTime());
        automobileOrder.setUpdateTime(automobileInfo.getUpdateTime());

        baseMapper.insert(automobileOrder);

        return automobileOrder.getOrderId();
    }

    @Override
    public Map createNative(String orderId) {
        return null;
    }

    //    生成二维码
//    @Override
//    public Map createNative(String orderId) {
//        try {
//            //根据订单id获取订单信息
//            QueryWrapper<AutomobileOrder> wrapper = new QueryWrapper<>();
//
//            wrapper.eq("order_id", orderId);
//            AutomobileOrder order = automobileOrderService.getOne(wrapper);
//            Map m = new HashMap();
////1、设置支付参数
//            m.put("appid", "wx74862e0dfcf69954");
//            m.put("mch_id", "1558950191");
//            m.put("nonce_str", WXPayUtil.generateNonceStr());
//            m.put("body", order.getAutomobileName());
//            m.put("out_trade_no", orderId);
//            m.put("total_fee", order.getOrderPrice());
//            m.put("spbill_create_ip", "127.0.0.1");
//            m.put("notify_url", "http://guli.shop/api/order/weixinPay/weixinNotify\n");
//            m.put("trade_type", "NATIVE");
////2、HTTPClient来根据URL访问第三方接口并且传递参数
//            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
////client设置参数
//            client.setXmlParam(WXPayUtil.generateSignedXml(m,
//                    "T6m9iK73b0kn9g5v426MKfHQH7X8rKwb"));
//            client.setHttps(true);
//            client.post();
////3、返回第三方的数据
//            String xml = client.getContent();
//            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
////4、封装返回结果集
//            Map map = new HashMap<>();
//            map.put("out_trade_no", orderId);
//            map.put("course_id", order.getAutomobileId());
//            map.put("total_fee", order.getOrderPrice());
//            map.put("result_code", resultMap.get("result_code"));
//            map.put("code_url", resultMap.get("code_url"));
////微信支付二维码2小时过期，可采取2小时未支付取消订单
////redisTemplate.opsForValue().set(orderNo, map, 120,TimeUnit.MINUTES);
//            return map;
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new HashMap<>();
//        }
//
//
//    }
    //    生成二维码
//    @Override
//    public Map createNative(String orderId) {
//        try {
//            //根据订单id获取订单信息
//            QueryWrapper<AutomobileOrder> wrapper = new QueryWrapper<>();
//
//            wrapper.eq("order_id", orderId);
//            AutomobileOrder order = automobileOrderService.getOne(wrapper);
//            Map m = new HashMap();
////1、设置支付参数
//            m.put("appid", "wx74862e0dfcf69954");
//            m.put("mch_id", "1558950191");
//            m.put("nonce_str", WXPayUtil.generateNonceStr());
//            m.put("body", order.getAutomobileName());
//            m.put("out_trade_no", orderId);
//            m.put("total_fee", order.getOrderPrice());
//            m.put("spbill_create_ip", "127.0.0.1");
//            m.put("notify_url", "http://guli.shop/api/order/weixinPay/weixinNotify\n");
//            m.put("trade_type", "NATIVE");
////2、HTTPClient来根据URL访问第三方接口并且传递参数
//            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
////client设置参数
//            client.setXmlParam(WXPayUtil.generateSignedXml(m,
//                    "T6m9iK73b0kn9g5v426MKfHQH7X8rKwb"));
//            client.setHttps(true);
//            client.post();
////3、返回第三方的数据
//            String xml = client.getContent();
//            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
////4、封装返回结果集
//            Map map = new HashMap<>();
//            map.put("out_trade_no", orderId);
//            map.put("course_id", order.getAutomobileId());
//            map.put("total_fee", order.getOrderPrice());
//            map.put("result_code", resultMap.get("result_code"));
//            map.put("code_url", resultMap.get("code_url"));
////微信支付二维码2小时过期，可采取2小时未支付取消订单
////redisTemplate.opsForValue().set(orderNo, map, 120,TimeUnit.MINUTES);
//            return map;
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new HashMap<>();
//        }


//    }


//    @Override
//    public Map<String, String> queryStatus(String orderId) {
//        try {
////1、封装参数
//            Map m = new HashMap<>();
//            m.put("appid", "wx74862e0dfcf69954");
//            m.put("mch_id", "1558950191");
//            m.put("out_trade_no", orderId);
//            m.put("nonce_str", WXPayUtil.generateNonceStr());
////2、设置请求
//            HttpClient client = new
//                    HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
//            client.setXmlParam(WXPayUtil.generateSignedXml(m,
//                    "T6m9iK73b0kn9g5v426MKfHQH7X8rKwb"));
//            client.setHttps(true);
//            client.post();
////3、返回第三方的数据
//            String xml = client.getContent();
//            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
////6、转成Map
////7、返回
//            return resultMap;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    @Override
//    public void updateOrderStatus(Map<String, String> map) {
////          从map获取订单号
//        String orderId = map.get("out_trade_no");
////        根据订单号查询订单信息
//        QueryWrapper<AutomobileOrder> wrapper = new QueryWrapper<>();
//        wrapper.eq("order_id", orderId);
//        AutomobileOrder order = automobileOrderService.getOne(wrapper);
//
////        更新订单表订单状态
//        if (order.getOrderStatus().intValue() == 1) {
//            return;
//        }
//        order.setOrderStatus(1);  //代表已支付
//        automobileOrderService.updateById(order);
//
////        向支付表添加支付记录
//        AutomobileOrderLog automobileOrderLog = new AutomobileOrderLog();
//
//
//
////        baseMapper.insert(automobileOrderLog);
//
//
//
//    }


}
